#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setCentralWidget(ui->splitter);
    ui->tBtnListInit->setDefaultAction(ui->actListInit);
    ui->tBtnListClear->setDefaultAction(ui->actListClear);
    ui->tBtnListAppend->setDefaultAction(ui->actItemAppend);
    ui->tBtnListDelete->setDefaultAction(ui->actItemDelete);
    ui->tBtnListInsert->setDefaultAction(ui->actItemInsert);

    ui->tBtnSelectIAll->setDefaultAction(ui->actSelectAll);
    ui->tBtnSelectIInv->setDefaultAction(ui->actSelectInvs);
    ui->tBtnSelectINone->setDefaultAction(ui->actSelectNone);

    createSelectionPopMenu();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actListInit_triggered()
{
    //初始化列表
    QListWidgetItem *item;
    QIcon icon;
    icon.addFile(":/images/icons/check2.ico");
    bool chk = ui->chkListEditable->isChecked();

    ui->listWidget->clear();
    for (int i = 0; i < 10; i++) {
        item = new QListWidgetItem();
        QString str = QString::asprintf("Item %d", i);
        item->setText(str);
        item->setIcon(icon);
        item->setCheckState(Qt::CheckState::Checked);
        if (chk) {
            item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |
                           Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        }
        else {
            item->setFlags(Qt::ItemIsSelectable |
                           Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        }
        ui->listWidget->addItem(item);
    }
}

void MainWindow::on_actItemInsert_triggered()
{
    //初始化列表
    QListWidgetItem *item = new QListWidgetItem("New Insert Item");
    QIcon icon;
    icon.addFile(":/images/icons/check2.ico");
    bool chk = ui->chkListEditable->isChecked();

    item->setIcon(icon);
    item->setCheckState(Qt::CheckState::Checked);
    if (chk) {
        item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |
                       Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    else {
        item->setFlags(Qt::ItemIsSelectable |
                       Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    ui->listWidget->insertItem(ui->listWidget->currentRow(), item);
}

void MainWindow::on_actItemDelete_triggered()
{
    QListWidgetItem *item = ui->listWidget->currentItem();
    ui->listWidget->takeItem(ui->listWidget->currentRow());
    delete item;
}

void MainWindow::on_actItemAppend_triggered()
{
    //初始化列表
    QListWidgetItem *item = new QListWidgetItem("New Append Item");
    QIcon icon;
    icon.addFile(":/images/icons/check2.ico");
    bool chk = ui->chkListEditable->isChecked();

    item->setIcon(icon);
    item->setCheckState(Qt::CheckState::Checked);
    if (chk) {
        item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |
                       Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    else {
        item->setFlags(Qt::ItemIsSelectable |
                       Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    ui->listWidget->insertItem(ui->listWidget->currentRow()+1, item);
}

void MainWindow::on_actListClear_triggered()
{
    ui->listWidget->clear();
}

void MainWindow::on_actSelectAll_triggered()
{
    for (int i = 0; i < ui->listWidget->count(); i++) {
        QListWidgetItem* item = ui->listWidget->item(i);
        item->setCheckState(Qt::CheckState::Checked);
    }
}

void MainWindow::on_actSelectNone_triggered()
{
    for (int i = 0; i < ui->listWidget->count(); i++) {
        QListWidgetItem* item = ui->listWidget->item(i);
        item->setCheckState(Qt::CheckState::Unchecked);
    }
}

void MainWindow::on_actSelectInvs_triggered()
{
    for (int i = 0; i < ui->listWidget->count(); i++) {
        QListWidgetItem* item = ui->listWidget->item(i);
        if (item->checkState() == Qt::CheckState::Checked)
            item->setCheckState(Qt::CheckState::Unchecked);
        else
            item->setCheckState(Qt::CheckState::Checked);
    }
}

void MainWindow::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    QString str;
    if (current != NULL) {
        if (previous != NULL) {
            str = "当前项:" + current->text() + ". 前一项:" + previous->text();
        }
        else
            str = "当前项:" + current->text();
    }
    ui->editCurItemText->setText(str);
}

void MainWindow::createSelectionPopMenu()
{
    //定义一个menu，包含3个action
    QMenu *menuSelection = new QMenu(this);
    menuSelection->addAction(ui->actSelectAll);
    menuSelection->addAction(ui->actSelectNone);
    menuSelection->addAction(ui->actSelectInvs);
    //listWidget中的项选择ToolButton
    ui->tBtnSelectItem->setPopupMode(QToolButton::MenuButtonPopup);
    ui->tBtnSelectItem->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    ui->tBtnSelectItem->setDefaultAction(ui->actItemSelect);
    ui->tBtnSelectItem->setMenu(menuSelection);
    //toolbar中的项选择ToolButton
    QToolButton *aBtn = new QToolButton(this);
    aBtn->setPopupMode(QToolButton::InstantPopup);
    aBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    aBtn->setDefaultAction(ui->actItemSelect);
    aBtn->setMenu(menuSelection);
    ui->menuToolBar->addWidget(aBtn);
    //ToolBar中添加
    ui->menuToolBar->addSeparator();
    ui->menuToolBar->addAction(ui->actExit);
}

void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
    Q_UNUSED(pos);
    QMenu *menuList = new QMenu(this);
    //为右击创建菜单
    menuList->addAction(ui->actListInit);
    menuList->addAction(ui->actListClear);
    menuList->addAction(ui->actItemInsert);
    menuList->addAction(ui->actItemAppend);
    menuList->addAction(ui->actItemDelete);
    menuList->addSeparator();
    menuList->addAction(ui->actSelectAll);
    menuList->addAction(ui->actSelectNone);
    menuList->addAction(ui->actSelectInvs);

    menuList->exec(QCursor::pos());

    delete menuList;
}
